import { _decorator,Vec2 } from 'cc';
import { playerState } from './playerState';
import { ENTITY_STATE_ENUM } from '../../Enum';
import { InputManager } from '../../Base/InputManager';
const { ccclass, property } = _decorator;

@ccclass('DashState')
export class DashState extends playerState {
    private _lastDashDir: Vec2 = new Vec2(0,0);

    dashAttackTime: number = 0; 
    dashEndTime: number = 0; 
    onEnter(): void {
        super.onEnter();
        
        const data = this.fsm.player.data;
        const move = InputManager.Instance.moveDir;
        this.dashAttackTime = data.dashAttackTime;  
        this.dashEndTime = data.dashEndTime;
        this.fsm.lastPressedDashTime = 0;
        this.fsm.lastOnGroundTime = 0;
        this.fsm.dashAmount--;
        this.fsm.setGravityScale(0);

        if (move.length() > 0) {
            this._lastDashDir.set(move.normalize());
        } else {
            this.fsm.isFacingRight ? this._lastDashDir.set(1,0) : this._lastDashDir.set(-1,0);
        }

        this.rb.linearVelocity = new Vec2(
            data.dashSpeed * this._lastDashDir.x,  // X方向速度 = dashSpeed * 方向X分量
            data.dashSpeed * this._lastDashDir.y   // Y方向速度 = dashSpeed * 方向Y分量
        );
        
        console.log("speed:",this.rb.linearVelocity);
    }

    update(deltaTime: number): void {
        super.update(deltaTime); 
        console.log("speed:",this.rb.linearVelocity);
        if(this.fsm.lastOnGroundTime > 0 && this.fsm.lastPressedJumpTime > 0){
            this.fsm.transit(ENTITY_STATE_ENUM.SUPER_JUMP);
        }else if(this.fsm.lastOnWallTime > 0 && this.fsm.lastPressedJumpTime > 0){
            this.fsm.transit(ENTITY_STATE_ENUM.SUPER_WALL_JUMP); 
        }
        // this.run(this.fsm.player.data.dashEndRunLerp);
    }

    *Coroutine(): Generator {
        yield this.dashAttackTime;
        // console.log('Dash Attack over', this.dashAttackTime);
        this.fsm.setGravityScale(this.fsm.player.data.gravityScale);
        this.rb.linearVelocity = new Vec2(
            this.fsm.player.data.dashEndSpeed.x * this._lastDashDir.x,
            this.fsm.player.data.dashEndSpeed.y * this._lastDashDir.y
        );
        yield this.dashEndTime;
        this.fsm.transit(ENTITY_STATE_ENUM.FALL);
    }
    IsCoroutine(): boolean {
        return true;
    }

    onExit(): void {
        super.onExit(); 
    }
}


